﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using System.Media;

namespace DoAnHuongRung
{
    public partial class frmThucDon : Form
    {
        public frmThucDon()
        {
            InitializeComponent();
        }

        private void frmThucDoc_Load(object sender, EventArgs e)
        {
            LoadCaLamViec();
            LoadMonAn();
            LoadDSThucDon();
        }

        private void LoadCaLamViec()
        {
            cbbCaLamViec.DisplayMember = "TenCa";
            cbbCaLamViec.ValueMember = "MaCa";
            cbbCaLamViec.DataSource = CaLamViecBUS.SelectAll();
        }
        private void LoadMonAn()
        {
            cbbMonAn.DisplayMember = "TenMonAn";
            cbbMonAn.ValueMember = "MaMonAn";
            cbbMonAn.DataSource = MonAnBUS.SelectAll();
        }
        private void LoadDSThucDon()
        {
            dgvThucDon.DataSource = ThucDonBUS.SelectAll();
        }
        private void LoadDSMonAn(string MaThucDon)
        {
            dgvMonAn.DataSource = ChiTietThucDonBUS.SelectAllByMaThucDon(Convert.ToInt32(txtMaThucDon.Text));
            nudSoPhanAn.Value = 0;
        }

        private void dgvThucDon_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvThucDon.CurrentCell != null)
            {
                txtMaThucDon.Text = dgvThucDon.CurrentRow.Cells[0].Value.ToString();
                dtpNgay.Text = dgvThucDon.CurrentRow.Cells[1].Value.ToString();
                cbbCaLamViec.Text = dgvThucDon.CurrentRow.Cells[3].Value.ToString();

                if (txtMaThucDon.Text != "")
                    LoadDSMonAn(txtMaThucDon.Text);
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            //Thêm
            bool b = ThucDonBUS.Insert(new ThucDonDTO(0, dtpNgay.Value.ToString("MM/dd/yyyy"), (int)cbbCaLamViec.SelectedValue));
            if (b == true)
            {
                LoadDSThucDon();
                new SoundPlayer(Properties.Resources.Windows_Notify).Play();
                MessageBox.Show("Đã thêm !!!");
            }
        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaThucDon.Text == "")
                MessageBox.Show("Chọn thực đơn cần sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            else
            {
                //Sửa
                bool b = ThucDonBUS.Update(new ThucDonDTO(Convert.ToInt32(txtMaThucDon.Text), dtpNgay.Value.ToString("MM/dd/yyyy"), (int)cbbCaLamViec.SelectedValue));
                if (b == true)
                {
                    LoadDSThucDon();
                    new SoundPlayer(Properties.Resources.Windows_Notify).Play();
                    MessageBox.Show("Đã sửa !!!");
                }
            }
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaThucDon.Text == "")
                MessageBox.Show("Chọn thực đơn cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            else
            {
                try
                {
                    //Xóa
                    bool b = ChiTietThucDonBUS.DeleteByMaThucDon(Convert.ToInt32(txtMaThucDon.Text));
                    bool c = ThucDonBUS.Delete(Convert.ToInt32(txtMaThucDon.Text));
                    if (b && c)
                    {
                        LoadDSThucDon();
                        new SoundPlayer(Properties.Resources.Windows_Notify).Play();
                        MessageBox.Show("Đã xóa !!!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi: " + ex.Message);
                }
            }
        }

        private void btnThemNVL_Click(object sender, EventArgs e)
        {
            if (txtMaThucDon.Text == "")
                MessageBox.Show("Chọn thực đơn");
            else if (nudSoPhanAn.Value == 0)
                MessageBox.Show("Đổi số phần ăn");
            else
            {
                if (ChiTietThucDonBUS.Insert(new ChiTietThucDonDTO(0,
                    Convert.ToInt32(txtMaThucDon.Text),
                    (int)cbbMonAn.SelectedValue,
                    (int)nudSoPhanAn.Value)))
                    LoadDSMonAn(txtMaThucDon.Text);
            }
        }
        private void btnXoaNVL_Click(object sender, EventArgs e)
        {
            if (txtMaThucDon.Text == "")
                MessageBox.Show("Chọn thực đơn");
            else
            {
                foreach (DataGridViewRow dr in dgvMonAn.SelectedRows)
                {
                    ChiTietThucDonBUS.Delete((int)dr.Cells[0].Value);
                    dgvMonAn.Rows.Remove(dr);
                }
            }
        }
    }
}
